<template>
  <div>
    <div class="title">
      <div class="serve">
        <div class="name">区域搜索:</div>
        <div>
          <el-input v-model="input" placeholder="请输入内容"></el-input>
        </div>
      </div>
      <div class="btn">
        <el-button type="primary" icon="el-icon-search" size="medium" @click="sever(input)">搜索</el-button>
      </div>
    </div>
    <div class="main">
      <div class="top-btn">
        <el-button type="warning" icon="el-icon-circle-plus-outline" size="medium" @click="addFn">新建</el-button
        >
      </div>
      <div class="count">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column fixed prop="index" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="name" label="区域名称" width="450">
          </el-table-column>
          <el-table-column prop="nodeCount" label="点位数" width="450">
          </el-table-column>
          <el-table-column prop="remark" label="备注说明" width="450">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button @click.native.prevent="detail(scope.$index, tableData)" type="text" size="small">
                查看详细
              </el-button>
              <el-button
                @click.native.prevent="revise(scope.$index, tableData)"
                type="text" size="small"> 修改</el-button>
              <el-button style="color: red" @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <Footer @next="next" @up="up" :number="number" :pageIndex="region.pageIndex"/>
    <Detail :visible.sync="reg.detail" :find ="find" :name="name"/>
    <Revise :visible.sync="reg.revise" :change ="this.reg.change" @abst="abst"/>
  </div>
</template>

<script>
import Footer from '@/components/public/FtooTer.vue'
import Detail from '@/views/getLevel/components/reg-detail.vue'
import Revise from '@/views/getLevel/components/reg-revise.vue'

import { search, deletes, node } from '@/api/getLevel/regIonal'
export default {
  components: {
    Footer,
    Detail,
    Revise
  },
  async created () {
    this.Data()
  },
  methods: {
    // 搜索
    async sever (input) {
      this.region.name = input
      this.Data()
    },
    // 新增
    addFn () {
      this.reg.revise = true
      this.reg.change.show = false
    },
    // 删除
    async deleteRow (index, rows) {
      await deletes(rows[index].id)
      this.Data()
    },
    // 修改
    revise (index, rows) {
      const res = rows[index]
      this.reg.change.show = true
      this.reg.change.name = res.name
      this.reg.change.reg = res.remark
      this.reg.change.id = res.id
      console.log(res, this.reg.change)
      this.reg.revise = true
    },
    // 查看
    async  detail (index, rows) {
      console.log(rows[index])
      this.reg.detail = true
      this.region.regionId = rows[index].id
      const { currentPageRecords } = await node(this.region)
      console.log(currentPageRecords)
      this.find = currentPageRecords
      this.name = rows[index].name
      console.log(this.find)
    },
    // 新增&查看
    abst () {
      this.Data()
    },
    // 下一页
    next () {
      this.region.pageIndex++
      // 页数
      this.Data()
    },
    // 上一页
    up () {
      this.region.pageIndex--
      // 页数
      this.Data()
    },
    // 接口渲染页面
    async Data () {
      const res = await search(this.region)
      this.tableData = res.currentPageRecords// 对获取的数据添加序号属性
      this.tableData.forEach((item, index) => {
        // 添加序号
        this.$set(item, 'index', index + 1 + ((this.region.pageIndex - 1) * 10))
      })
      // 总条数
      this.number = res.totalCount
    }
  },
  data () {
    return {
      // 增删改查
      reg: {
        detail: false,
        revise: false,
        change: {
          show: '',
          name: '',
          reg: '',
          id: ''
        }
      },
      input: '',
      number: '', // 总条数
      now: '', // 当前页数
      // 请求参数
      region: {
        pageIndex: 1,
        pageSize: 10,
        name: '',
        regionId: ''
      },
      tableData: [],
      find: [],
      name: ''
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  width: 100%;
  background-color: #fff;
  height: 9.5%;
  margin-bottom: 1.2%;
  display: flex;
  line-height: 64px;
  .serve {
    display: flex;
    .name {
      width: 35%;
      font-size: 14px;
    }
  }
}
.main {
  height: 90%;
  width: 100%;
  background-color: #fff;
  .top-btn {
    display: flex;
    width: 100%;
    height: 7%;
    margin-left: 10px;
    margin-bottom: 16px;
    // background-color: red;
  }
  .count {
    height: 93%;
  }
}
</style>
